/**
 * jQuery JavaScript dialog
 * http://www.citybao.net
 *
 * Copyright 2010, xydream
 * Released under the MIT, BSD, and GPL Licenses.
 *
 * Date: Sat May 3 22:33:48 2010 -0500
 *  
 * Params:
 * title:	窗口标题
 * content: 格式:"类型：内容"（类型可选类型：text | id | img | url | iframe）
 * width:	窗口宽度
 * height:	窗口高度
 * drag:    是否可以拖动(ture为是,false为否)
 * time:    自动关闭等待的时间，为空是则不自动关闭
 * showbg:  [可选参数]设置是否显示遮罩层(0为不显示,1为显示)
 * cssName: [可选参数]附加class名称
 * 
 * Use : CB_ImgPath
 *
 * Includes CityBao.css
 *
 * Example:
 * Dialog("Example","text:This is a example","500","400",true,"3000",true,"exa");
 */
var MaxWindowWidth  = 950; //窗口最大宽度
var MaxWindowHeight = 525; //窗口最大高度
var BackgroundOpacity = "0.5";//背景透明度
var showDialog = true;
function Dialog(title,content,width,height,drag,time,showbg,cssName) {
	$("#Dialog-box").remove(); 
	var width = width>= MaxWindowWidth?this.width=MaxWindowWidth:this.width=width;	   
	var height = height>= MaxWindowHeight?this.height=MaxWindowHeight:this.height=height; 
	if(showDialog == true) {
		var Dialog_html = new String;
		Dialog_html = "<div id=\"Dialog-bg\" style=\"height:"+$(document).height()+"px;filter:alpha(opacity="+BackgroundOpacity+");opacity:"+BackgroundOpacity+";z-index: 999\"></div>";
		Dialog_html += "<table class=\"pop_dialog_table\" id=\"Dialog-box\" style=\"z-index: 1024; position: fixed; \"><tbody>";
		Dialog_html += "<tr><td class=\"pop_topleft\"></td><td class=\"pop_border pop_top\"></td><td class=\"pop_topright\"></td></tr>";
		Dialog_html += "<tr><td class=\"pop_border pop_side\"></td><td id=\"pop_content\" class=\"pop_content\">";
		Dialog_html += "<div id=\"Dialog-title\" class=\"dialog_title\"><h5></h5><span id=\"Dialog-close\">关闭</span></div>";
		Dialog_html += "<table id=\"Dialog-content-wrap\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td valign=\"middle\" id=\"Dialog-content\"></td></tr></tbody></table>";
		//Dialog_html += "<div id=\"dialog_button\" class=\"dialog_button1\"><a href=\"javascript:;\"><input class=\"saveok\" type=\"button\" name=\"\" value=\"确定\"><span style=\"display:none;\">确定</span></a></div>";
		Dialog_html += "</td><td class=\"pop_border pop_side\"></td></tr>";
		Dialog_html += "<tr><td class=\"pop_bottomleft\"></td><td class=\"pop_border pop_bottom\"></td><td class=\"pop_bottomright\"></td></tr>";
		Dialog_html += "</tbody></table>";
		$("body").append(Dialog_html);
		show = false;
	}
	contentType = content.substring(0,content.indexOf(":"));
	content = content.substring(content.indexOf(":")+1,content.length);
	
	switch(contentType) {
		case "text":
			$("#Dialog-content").html(content);
			break;
		case "id":
			$("#Dialog-content").html($("#"+content+"").html());
			break;
		case "img":
			$("#Dialog-content").ajaxStart(function() {
				$(this).html("<img src='"+CB_ImgPath+"ajaxloading.gif' class='loading' />");
			});
			$.ajax({
				error:function(){
					$("#Dialog-content").html("<p class='Dialog-error'>加载数据出错...</p>");
				},
				success:function(html){
					$("#Dialog-content").html("<img src="+content+" alt='' />");
				}
			});
			break;
		case "url":
			var content_array=content.split("?");
			$("#Dialog-content").ajaxStart(function(){
				$(this).html("<img src='"+CB_ImgPath+"ajaxloading.gif' class='loading' />");
			});
			$.ajax({
				type:content_array[0],
				url:content_array[1],
				data:content_array[2],
				error:function(){
					$("#Dialog-content").html("<p class='Dialog-error'>加载数据出错...</p>");
				},
				success:function(html){
					$("#Dialog-content").html(html);
				}
			});
			break;
		case "iframe":
			$("#Dialog-content").ajaxStart(function(){
				$(this).html("<img src='"+CB_ImgPath+"ajaxloading.gif' class='loading' />");
			});
			$.ajax({
				error:function(){
					$("#Dialog-content").html("<p class='Dialog-error'>加载数据出错...</p>");
				},
				success:function(html){
					$("#Dialog-content").html("<iframe src=\""+content+"\" width=\"100%\" height=\""+parseInt(height)+"px"+"\" scrolling=\"auto\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"></iframe>");
				}
			});
	}
	$("#Dialog-title h5").html(title);
	if(showbg == true) {
		$("#Dialog-bg").show();
	}else {	
		$("#Dialog-bg").remove();
	};
	$("#Dialog-bg").animate({opacity:BackgroundOpacity},"normal");
	$("#Dialog-box").show();
	if( height >= MaxWindowHeight ) {
		$("#Dialog-box").css({width:(parseInt(width)+20)+"px",height:height+"px"});
		$("#Dialog-content-wrap").css({width:(parseInt(width)-20)+"px",height:(parseInt(height)-45)+"px"});		
	}else {
		$("#Dialog-box").css({width:width+"px",height:height+"px"});
		$("#Dialog-content-wrap").css({width:(parseInt(width)-20)+"px",height:(parseInt(height)-45)+"px"});
	}
	var	cw = document.documentElement.clientWidth,ch = document.documentElement.clientHeight,est = document.documentElement.scrollTop; 
	var _version = $.browser.version;
	if ( _version == 6.0 ) {
		$("#Dialog-box").css({left:"50%",top:(parseInt((ch)/2)+est)+"px",marginTop: -((parseInt(height)+53)/2)+"px",marginLeft:-((parseInt(width)+32)/2)+"px",zIndex: "999999"});
	}else {
		$("#Dialog-box").css({left:"50%",top:"50%",marginTop:-((parseInt(height)+53)/2)+"px",marginLeft:-((parseInt(width)+32)/2)+"px",zIndex: "999999"});
	};
	var Drag_ID = $("#Dialog-box"),DragHead = $("#Dialog-title");
	var moveX = 0,moveY = 0,moveTop,moveLeft = 0,moveable = false;
	if ( _version == 6.0 ) {
		moveTop = est;
	}else {
		moveTop = 0;
	}
	var	sw = Drag_ID[0].scrollWidth,sh = Drag_ID[0].scrollHeight;
	DragHead.mouseover( function(e) {
		if(drag == true){
			DragHead.css('cursor',"move");
		}else{
			DragHead.css('cursor',"default");
		}
	});
	DragHead.mousedown(function(e) {
		moveable = drag == true ? true : false;
		e = window.event?window.event:e;
		var ol = Drag_ID.offset().left, ot = Drag_ID.offset().top-moveTop;
		moveX = e.clientX-ol;
		moveY = e.clientY-ot;
		document.onmousemove = function(e) {
			if (moveable) {
				e = window.event?window.event:e;
				var x = e.clientX - moveX;
				var y = e.clientY - moveY;
				if ( x > 0 &&( x + sw < cw) && y > 0 && (y + sh < ch) ) {
					Drag_ID.css('left', x + "px");
					Drag_ID.css('top', parseInt(y+moveTop) + "px");
					Drag_ID.css('margin', "auto");
				}
			}
		};
		document.onmouseup = function () {
			moveable = false;
		};
		Drag_ID[0].onselectstart = function(e){
			return false;
		}
	});
	$("#Dialog-content").attr("class","Dialog-"+cssName);
	var closeDialog = function() {
		$("#Dialog-bg").remove();
		$("#Dialog-box").fadeOut("slow",function(){$(this).remove();});
	}
	if( time == "" || typeof(time) == "undefined") {
		;
	}else { 
		setTimeout(closeDialog,time);
	}
	$("#Dialog-close").click(function() {
		$("#Dialog-bg").remove();
		$("#Dialog-box").fadeOut("slow",function(){$(this).remove();});
	});
}
